<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 10/8/14
 * Time: 9:28 PM
 */

namespace Backend\Modules\User\Controllers;
use Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Library\Constant\Constant;
use Backend\Modules\User\Forms\FormCreatePermissionResource;
use Backend\Modules\User\Models\PermissionResource;

class PermissionResourcesController extends \BackendController
{
    public $urlPermissionResource = 'user/permission-resources';
    public function indexAction()
    {

        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $permissionResource = PermissionResource::find();
        $data = array();

        if ($currentPage == 0) {
            $currentPage = 1;
        }
        $pager = new Pager(
            new Paginator(array(
                'data'  => $permissionResource,
                'limit' => Constant::ITEM_PER_PAGE_20,
                'page'  => $currentPage,
            )),
            array(
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                // Range window will be 5 pages
                'rangeLength' => 5,
                // Just a string with URL mask
                'urlMask'     => '',
            )
        );

        $this->view->pager = $pager;
    }

    /*
     * create
     */
    public function createAction()
    {
        $form = new FormCreatePermissionResource();
        $form->setAction($this->getBackendUrl() . '/' . $this->urlPermissionResource . '/create');
        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $permissionResouce = new PermissionResource();
                $permissionResouce->assign($this->request->getPost());
                if($permissionResouce->save())
                {
                    return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlPermissionResource);
                }
                $this->flash->error($permissionResouce->getMessages());
            }
        }
        $this->view->form = $form;
    }

    /*
     * create
     */
    public function editAction($id)
    {
        $permissionResouce = PermissionResource::findFirstById($id);
        if(!$permissionResouce)
        {
            $this->flash->error("PermissionResource was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }

        $form = new FormCreatePermissionResource($permissionResouce , array('edit' => true));
        $form->setAction($this->getBackendUrl() . '/' . $this->urlPermissionResource . '/edit/' . $id);
        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $permissionResouce->assign($this->request->getPost());
                if($permissionResouce->update())
                {
                    return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlPermissionResource);
                }
                $this->flash->error($permissionResouce->getMessages());
            }
        }
        $this->view->form = $form;
    }

    public function deleteAction($id)
    {
        $permissionResouce = PermissionResource::findFirstById($id);
        if(!$permissionResouce)
        {
            $this->flash->error("PermissionResource was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }

        if(!$permissionResouce->delete())
        {
            $this->flash->error($permissionResouce->getMessages());
        }
        else
        {
            $this->flash->success("Permission Resouce was deleted");
        }

        return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlPermissionResource);
    }
}